//
//  PlatformServer.m
//  XTCloud
//
//  Created by QA on 2018/1/10.
//  Copyright © 2018年 ShangHai HaoLei Software Technology Co., Ltd. All rights reserved.
//

#import "PlatformServer.h"
#import "HttpPlatformConnect.h"

@implementation PlatformServer

static PlatformServer *sharedService = nil;
+ (PlatformServer*)sharedService
{
    @synchronized(self){
        if (sharedService == nil){
            sharedService = [[self alloc] init];
        }
    }
    return sharedService;
}

- (instancetype) init {
    self = [super init];
    if(self) {
        self.adInfoArray = [NSArray array];
    }
    return self;
}

#pragma mark 查询广告信息

- (void)startQueryADService
{
    HttpPlatformInputRequest *inputRequest = [[HttpPlatformInputRequest alloc] init];
    [inputRequest setInterface:@"XTCLOUD_AD"];
    
    HttpPlatformConnect *content = [HttpPlatformConnect shareInstance];
    [content requestWithParams:inputRequest success:^(HttpPlatformOutputResult * responseObject) {
        NSLog(@"查询广告信息成功:%@",responseObject);
        NSArray *array = responseObject.objectArray;
        self.adInfoArray = array;
        
        //通知页面更新
        NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
        NSNotification *broadcastMessage = [NSNotification notificationWithName:PlatformADServiceKey object:array];
        [notificationCenter postNotification:broadcastMessage];
        
    } failure:^(NSError *error) {
        NSLog(@"==系统繁忙或异常，将会在20秒后重新尝试连接===%@",error);
        //延迟20秒，重新查询数据
        __block PlatformServer *weakSelf = self;
        dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(20.0 * NSEC_PER_SEC));
        dispatch_after(delayTime, dispatch_get_main_queue(), ^{
            [weakSelf startQueryADService];
        });
    }];
}

#pragma mark 广告统计提交

- (void)updateADVisitsID:(NSString*)adID
{
    HttpPlatformInputRequest *inputRequest = [[HttpPlatformInputRequest alloc] init];
    [inputRequest setInterface:@"XTCLOUD_AD_VISITS"];
    [inputRequest setParameterVale:adID key:@"广告ID"];
    
    HttpPlatformConnect *content = [HttpPlatformConnect shareInstance];
    [content requestWithParams:inputRequest success:^(HttpPlatformOutputResult * responseObject) {
        NSLog(@"查询广告信息成功");
        
    } failure:^(NSError *error) {
        NSLog(@"==系统繁忙或异常:%@",error);
    }];
}

@end
